package business.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import model.TUser;
import model.VUser;
import business.basic.iHibBaseDAO;
import business.basic.iHibBaseDAOImpl;
import business.dao.UserDAO;

@Component("userdao")
public class UserDAOImpl implements UserDAO {
	
	private iHibBaseDAO bdao = null;

	public UserDAOImpl() {
		bdao = new iHibBaseDAOImpl();
	}

	public void setBdao(iHibBaseDAO bdao) {
		this.bdao = bdao;
	}


	@Override
	public boolean add(TUser user) {
		Object obj = bdao.insert(user);
		if(obj!=null){
			return true;
		}
		return false;
	}

	@Override
	public boolean update(TUser user) {
		return bdao.update(user);
	}

	@Override
	public boolean delete(String userid) {
		return bdao.delete(TUser.class, userid);
	}

	@Override
	public TUser getTUserById(String userid) {
		return (TUser)bdao.findById(TUser.class, userid);
	}

	@Override
	public VUser getVUserById(String userid) {
		return (VUser)bdao.findById(VUser.class, userid);
	}

	@Override
	public List<TUser> getTUser() {
		String hql = "from TUser";
		return bdao.select(hql);
	}

	@Override
	public List<TUser> getTUserByWhere(String wherecondition, int startPage,
			int pageSize) {
		String hql = "from TUser";
		if(wherecondition!=null && !wherecondition.equals("")){
			hql += wherecondition;
		}
		return bdao.selectByPage(hql, startPage, pageSize);
	}

	@Override
	public int getAmounTUserByWhere(String wherecondition) {
		String hql = "select count(*) from TUser";
		if (wherecondition != null && !wherecondition.equals("")) {
			hql += wherecondition;
		}
		return bdao.selectValue(hql);
	}

	@Override
	public VUser loginUser(String userid, String pwd) {
		String hql = "from VUser where userid=? and pwd=?";
		Object[] para = {userid,pwd};
		List list = bdao.select(hql, para);
		if(list!=null &&list.size()>0){
			return (VUser)list.get(0);
		}
		return null;
	}

	@Override
	public List<VUser> getVUserByWhere(String wherecondition, int startPage,
			int pageSize) {
		String hql = "from VUser";
		if(wherecondition!=null && !wherecondition.equals("")){
			hql += wherecondition;
		}
		return bdao.selectByPage(hql, startPage, pageSize);
	}

	@Override
	public int getAmounVUserByWhere(String wherecondition) {
		String hql = "select count(*) from VUser";
		if (wherecondition != null && !wherecondition.equals("")) {
			hql += wherecondition;
		}
		return bdao.selectValue(hql);
	}

}
